Protected WithEvents dlstBooks As System.Web.UI.WebControls.DataList
Protected WithEvents btnDisplay As System.Web.UI.WebControls.Button
Protected WithEvents txtPath As System.Web.UI.WebControls.TextBox
Protected WithEvents btnDisplayFileNames As System.Web.UI.WebControls.Button
Protected WithEvents rptFilenames As System.Web.UI.WebControls.Repeater
Protected WithEvents dlstFiles As System.Web.UI.WebControls.DataList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
' display the list of book the first time the page is loaded
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
BindBookList()
End If
End Sub
' close the connection when the page unloads
Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
' Close the connection if still open
If cn.State = ConnectionState.Open Then cn.Close()
End Sub
' The connection to the Pubs database.
Dim cn As New OleDbConnection(OledbPubsConnString)
Sub BindBookList()
' Open a connection to Pubs, if necessary.
If cn.State = ConnectionState.Closed Then cn.Open()
' Read title name and key from the Titles table.
Dim sql As String = "SELECT title_id, title FROM Titles"
' If we are in select or edit mode, we must read more data.
If dlstBooks.SelectedIndex >= 0 Or dlstBooks.EditItemIndex >= 0 Then
sql = "SELECT Titles.*, Publishers.pub_name FROM Titles " _
& "INNER JOIN Publishers ON Titles.pub_id=Publishers.pub_id"
End If
Dim cmd As New OleDbCommand(sql, cn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' Bind the DataReader to the DataList control.
dlstBooks.DataSource = dr
dlstBooks.DataKeyField = "title_id"
dlstBooks.DataBind()
dr.Close()
' if in edit mode, we must also fill the ddlPublisher DropDownList control
If dlstBooks.EditItemIndex >= 0 Then
' Get a reference to the DataListItem.
Dim dli As DataListItem = dlstBooks.Items(dlstBooks.EditItemIndex)
' Get a reference to the ddlPublishers DropDownList control.
Dim ddlPublishers As DropDownList = DirectCast(dli.FindControl("ddlPublishers"), DropDownList)
' Get a reference to the PubId hidden Literal control
Dim litPubId As Literal = DirectCast(dli.FindControl("litPubId"), Literal)
' Fill the list of publishers.
cmd = New OleDbCommand("SELECT pub_id, pub_name FROM Publishers", cn)
dr = cmd.ExecuteReader
ddlPublishers.DataSource = dr
ddlPublishers.DataTextField = "pub_name"
ddlPublishers.DataValueField = "pub_id"
ddlPublishers.DataBind()
dr.Close()
' Highlight the element corresponding to the title's publisher.
SelectItemFromValue(ddlPublishers, litPubId.Text)
End If
End Sub
' this event handler fires when a new DataList row is selected
Private Sub dlstBooks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dlstBooks.SelectedIndexChanged
' End the edit operation, if any.
dlstBooks.EditItemIndex = -1
BindBookList()
End Sub
' this event handler fires when the Edit button is clicked
Private Sub dlstBooks_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.EditCommand
' Hide any selected item, if any, and select the current item
dlstBooks.SelectedIndex = -1
dlstBooks.EditItemIndex = e.Item.ItemIndex
BindBookList()
End Sub
' this event handler fires when the Cancel button is clicked
Private Sub dlstBooks_CancelCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.CancelCommand
' End any edit operation.
dlstBooks.EditItemIndex = -1
BindBookList()
End Sub
' this event handler fires when the Delete button is clicked
Private Sub dlstBooks_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.DeleteCommand
' Retrieve the ID of the record to be deleted.
Dim id As String = dlstBooks.DataKeys(e.Item.ItemIndex).ToString
' Open the connection if necessary.
If cn.State = ConnectionState.Closed Then cn.Open()
' Delete this row.
Dim cmd As New OleDbCommand("DELETE Titles WHERE title_id='" & id & "'", cn)
'cmd.ExecuteNonQuery()
' Rebind the control.
BindBookList()
End Sub
' this event handler fires when the Update button is clicked
Private Sub dlstBooks_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.UpdateCommand
' Get the ID of the record to be deleted.
Dim title_id As String = dlstBooks.DataKeys(e.Item.ItemIndex).ToString
' This is the DataListItem being edited.
Dim dli As DataListItem = dlstBooks.Items(e.Item.ItemIndex)
' Get the values of the txtTitle child control.
Dim tb As TextBox = DirectCast(dli.FindControl("txtTitle"), TextBox)
Dim title As String = tb.Text
' Do the same with the txtType and txtPrice controls, but use a shorter syntax.
Dim type As String = DirectCast(dli.FindControl("txtType"), TextBox).Text
Dim price As Decimal = CDec(DirectCast(dli.FindControl("txtPrice"), TextBox).Text)
' Get the value of the selected element in ddlPublishers
Dim ddlPublishers As DropDownList = DirectCast(dli.FindControl("ddlPublishers"), DropDownList)
Dim pub_id As String = ddlPublishers.SelectedItem.Value
' Prepare to update this record.
Dim cmd As New OleDbCommand("UPDATE Titles SET title=?, pub_id=?, type=?, price=? WHERE title_id=?", cn)
cmd.Parameters.Add("@title", title)
cmd.Parameters.Add("@pub_id", pub_id)
cmd.Parameters.Add("@type", type)
cmd.Parameters.Add("@price", price)
cmd.Parameters.Add("@title_id", title_id)
' Open the connection if necessary, and execute the command
If cn.State = ConnectionState.Closed Then cn.Open()